﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataContexts.Supplier;
using System.Text;
using System.Web.UI.HtmlControls;

public partial class SearchResult : SearchBookingBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        divNoRecordMsg.Visible = false;
        LoadSupplierVehicles();

    }
    protected void rptSearchResult_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            ResultRow result = (ResultRow)e.Item.DataItem;

            Label lblVehicleCategory = (Label)e.Item.FindControl("lblVehicleCategory");
            Label lblAdultCapacity = (Label)e.Item.FindControl("lblAdultCapacity");
            Label lblRentPrice = (Label)e.Item.FindControl("lblRentPrice");
            HyperLink hypTermsConditions = (HyperLink)e.Item.FindControl("hypTermsConditions");
            Image imgVehicleImage = (Image)e.Item.FindControl("imgVehicleImage");

            Image imgTransimission = (Image)e.Item.FindControl("imgTransimission");
            Label lblTransimission= (Label)e.Item.FindControl("lblTransimission");
            Label lblAirConditioner = (Label)e.Item.FindControl("lblAirConditioner");
            Label lblNoOfDoors = (Label)e.Item.FindControl("lblNoOfDoors");
            
            Label lblLuggageCapacity = (Label)e.Item.FindControl("lblLuggageCapacity");
            Label lblDriverAge = (Label)e.Item.FindControl("lblDriverAge");
            HtmlAnchor hypBooking = (HtmlAnchor)e.Item.FindControl("hypBooking");
            
            imgVehicleImage.ImageUrl = "VehicleImage.ashx?vid=" + result.SupplierVehicleId.ToString();
            lblVehicleCategory.Text = result.Vehicle.VehicleCategory + " - " + result.Vehicle.VehicleDescription;
            lblAdultCapacity.Text = "x " + (result.Vehicle.AdultCapacity + result.Vehicle.ChildrenCapacity).ToString();
            lblLuggageCapacity.Text = "x " + result.Vehicle.LaggageCapacity.ToString();
            lblNoOfDoors.Text = "x " + result.Vehicle.NoOfDoors.ToString();

            lblAirConditioner.Text = (result.Vehicle.AirCondition.HasValue && result.Vehicle.AirCondition.Value ? "Yes" : "No");
            imgTransimission.ImageUrl = (result.Vehicle.AutoTransmission.HasValue && result.Vehicle.AutoTransmission.Value ? Helper.ApplicationPath + "Images/" + "Automatic.jpg" : Helper.ApplicationPath + "Images/" + "Manual.jpg");
            lblTransimission.Text = (result.Vehicle.AutoTransmission.HasValue && result.Vehicle.AutoTransmission.Value ? "Automatic" : "Manual");
            lblDriverAge.Text = "Driver Age Range: " + result.Vehicle.MinAge + " to " + result.Vehicle.MaxAge;

            lblRentPrice.Text = "Price: <br/>£" + result.Total.Value.ToString(Constants.DECIMAL_FORMAT);
            hypTermsConditions.NavigateUrl = "TermsAndConditions.aspx";

            string url = "/CarBookingDetails.aspx" + Helper.GetRequestUrl("|cpi|ps|") + "&slvid=" + result.SupplierLocationVehicleId.ToString() + "&svid=" + result.SupplierVehicleId.ToString() + "&sid=" + result.SupplierId.ToString();
            hypBooking.HRef = url;
        }
    }

    private void LoadSupplierVehicles()
    {
        SearchQuery query = new SearchQuery
                            {
                                CountryId = CountryId,
                                RegionId = RegionId,
                                LocationId = LocationId,
                                PickUpDate = PickUpDate,
                                DropDate = DropDate,
                                PickUpTime = PickUpTime,
                                DropTime = DropTime,
                                AdultCapacity = AdultCapacity,
                                ChildrenCapacity = ChildrenCapacity,
                                LuggageCapacity = LuggageCapacity,
                                ACOption = ACOption,
                                Transmission = Transmission,
                                PageSize = PageSize,
                                CurrentPageIndex = CurrentPageIndex
                            };

        SearchResults results = query.Execute();
        if (results.Rows.Count > 0)
        {
            rptSearchResult.DataSource = results.Rows;
            rptSearchResult.DataBind();

            divTopPagination.InnerHtml = GetPaginationLinks(CurrentPageIndex, (PageSize == 0 ? Constants.DEFAULT_PAGESIZE : PageSize), results.TotalRecords);
            divBottomPagination.InnerHtml = divTopPagination.InnerHtml;
            divSearchCriteria.Visible = true;
            lblNumberOfDays.Text = "Booking duration: " + supplierContext.GetTripDays(PickUpDate, DropDate,0).ToString() + " days";
        }
        else
        {
            divNoRecordMsg.Visible = true;
            divTopPagination.Visible = false;
            divBottomPagination.Visible = false;
        }
    }

    private string GetPaginationLinks(int currentPageIndex, int pageSize, int totalRecords)
    {
        StringBuilder builder = new StringBuilder();
        string url = "/searchresult.aspx" + Helper.GetRequestUrl("|cpi|") + "&cpi=";
        int pages = Convert.ToInt32(Math.Ceiling((Convert.ToDecimal(totalRecords) / Convert.ToDecimal(pageSize))));
        for (int iPage = 1; iPage <= pages; iPage++)
        {
            if (iPage == currentPageIndex)
                builder.AppendLine("<a href=\"#\" class=\"aSelPage\" >" + iPage.ToString() + "</a> ");
            else
                builder.AppendLine("<a href=\"" + url + iPage.ToString() + "\" >" + iPage.ToString() + "</a> ");
        }
        return builder.ToString();
    }
}
